#include "MyWiFi.h"
#include <ESP8266WiFi.h>
#include "Common.h"

/**
 * @brief 初始化AP模式/热点模式
 * @param  
 */
void MyWiFi::initAP(void)
{
    WiFi.mode(WIFI_AP);
    IPAddress local_IP(192, 168, 8, 1);
    IPAddress gateway(192, 168, 8, 1);
    IPAddress subnet(255, 255, 255, 0);
    WiFi.softAPConfig(local_IP, gateway, subnet); // 设置WiFi(IP、网关、掩码)
    WiFi.softAP("SWITCH"); 
    
    LOG("[MyWiFi] AP Mode init. name=[SWITCH] ip=[192.168.8.1]");
}

/**
 * @brief 初始化STA模式/连接WiFi
 * @param  
 */
void MyWiFi::initSTA(void)
{
    WiFi.mode(WIFI_STA);
    WiFi.begin(_ssid, _pwd);

    LOG_F("[MyWiFi] STA Mode init. ssid=[%s] pwd=[%s]\n", _ssid, _pwd);
}

/**
 * @brief 构造方法/初始化
 * @param  
 */
MyWiFi::MyWiFi(void)
{
    _timeCout = 0;
    _wifiStatus = WIFI_STATUS::STA_CONFIG;
    _cmd = WIFI_CMD::NOP;
}

/**
 * @brief 析构方法
 * @param  
 */
MyWiFi::~MyWiFi(void)
{
}

/**
 * @brief 状态机
 * @param  
 */
void MyWiFi::stateMachine(void)
{
    switch (_wifiStatus)
    {
    // AP配置模式：（热点模式），配置完跳转到AP运行中
    case WIFI_STATUS::AP_CONFIG: // AP配置
        initAP();
        _wifiStatus = WIFI_STATUS::AP_RUNNING;
        break;

    // AP运行中状态：监听其他Task发送的命令
    case WIFI_STATUS::AP_RUNNING: // AP运行中
        // LOG("[MyWiFi] AP_RUNNING");
        if (_cmd != WIFI_CMD::NOP)
        {
            _cmd = WIFI_CMD::NOP;

            // 收到连接WiFi的请求
            if (_cmd == WIFI_CMD::STA_CONFIG)
            {
                _wifiStatus = WIFI_STATUS::STA_CONFIG;
            }
            else if (_cmd == WIFI_CMD::STA_DISCONNECT)
            {
                WiFi.disconnect();
                _wifiStatus = WIFI_STATUS::AP_CONFIG;
            }
        }
        break;

    // STA配置状态：
    case WIFI_STATUS::STA_CONFIG: // STA配置
        LOG("[MyWiFi] STA_CONFIG");
        initSTA();
        _wifiStatus = WIFI_STATUS::STA_CONNECTING;
        break;

    // STA运行状态：监视wifi连接状态，断开则跳转到重连状态
    case WIFI_STATUS::STA_RUNNING: // STA运行中
        // LOG("[MyWiFi] STA_RUNNING");
        if (WiFi.status() != WL_CONNECTED)
        {
            _wifiStatus = WIFI_STATUS::STA_RECONNECT;
        }
        break;

    // STA重连状态
    case WIFI_STATUS::STA_RECONNECT: // STA断线重连
        LOG("[MyWiFi] STA_RECONNECT");
        WiFi.reconnect();
        _wifiStatus = WIFI_STATUS::STA_CONNECTING;
        break;

    // STA连接中状态
    case WIFI_STATUS::STA_CONNECTING: // 连接中
        LOG("[MyWiFi] STA_CONNECTING");
        if (WiFi.status() == WL_CONNECTED)
        {
            _timeCout = 0; // 清除计时器
            _wifiStatus = WIFI_STATUS::STA_RUNNING;
        }
        else
        {
            if (_timeCout > WIFI_TIME_OUT)
            {
                LOG("[MyWiFi] connect timeout!");
                _timeCout = 0; // 清除计时器
                _wifiStatus = WIFI_STATUS::AP_CONFIG;
            }
            else
            {
                _timeCout += WIFI_CYCLE;
            }
        }
        break;

    default:
        _wifiStatus = WIFI_STATUS::STA_CONFIG;
        break;
    }
}

/**
 * @brief 发送指令
 * @param cmd 指令
 */
void MyWiFi::sendCmd(WIFI_CMD cmd)
{
    _cmd = cmd;
}

/**
 * @brief 配置参数
 * @param ssid WiFi名称
 * @param pwd  WiFi密码
 */
void MyWiFi::configArg(String ssid, String pwd)
{
    _ssid = ssid;
    _pwd = pwd;
}
